﻿@page
@model Remotely.Server.Areas.Identity.Pages.Account.Manage.ApiTokensModel
@inject IDataService  DataService
@{
    ViewData["Title"] = "API Access Tokens";
}

<h4>@ViewData["Title"]</h4>

@if (!string.IsNullOrWhiteSpace(Model.Message))
{
    <h5 class="text-success mb-4 mt-4">@Model.Message</h5>
}

@if (!string.IsNullOrWhiteSpace(Model.NewTokenSecret))
{
    <h5 class="text-warning font-weight-bold mb-2 mt-4">Warning: The token secret will only be shown once.  Save it now.</h5>
    <h5>
        <label>Token Key: </label>
        <input class="form-control custom-control-inline" readonly value="@Model.NewTokenKey" style="width:400px" />
    </h5>
    <h5 class="mb-4">
        <label>Token Secret: </label>
        <input class="form-control custom-control-inline" readonly value="@Model.NewTokenSecret" style="width:400px" />
    </h5>
}


@if (Model.IsAdmin)
{
<form method="post" asp-page-handler="Create" class="mb-2 mt-4">
    <label class="mr-1">New Token Name:</label>
    <input class="form-control form-control-sm custom-control-inline mr-1" style="width:200px" asp-for="Input.TokenName" />
    <button class="btn btn-primary" type="submit">Create</button>
    <span data-toggle="modal" data-target="#apiTokensModal" class="fas fa-question-circle pointer"></span>
</form>

    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Token</th>
                <th>Last Used</th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var apiToken in Model.ApiTokens)
            {
                <tr>
                    <td>@apiToken.Name</td>
                    <td>@apiToken.Token</td>
                    <td>@apiToken.LastUsed</td>
                    <td>
                        <form method="post" class="rename-form" asp-page-handler="Rename">
                            <input hidden readonly asp-for="Input.TokenId" value="@apiToken.ID" />
                            <input hidden readonly asp-for="Input.TokenName" value="@apiToken.Name" />
                            <button type="submit" class="btn btn-primary">Rename</button>
                        </form>
                    </td>
                    <td>
                        <form method="post" class="delete-form" asp-page-handler="Delete">
                            <input hidden readonly asp-for="Input.TokenId" value="@apiToken.ID" />
                            <button type="submit" class="btn btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <div id="apiTokensModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Using API Tokens</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                </div>
                <div class="modal-body">
                    <p>
                        API tokens should be added to the request header when making API calls.  The key should be "Authorization", and value should be "{token}:{secret}".
                    </p>
                    <p>
                        Example: <span class="code">Authorization=e5da1c09-e851-4bd4-a8c1-532144b3f894:2xbA3TLPodNTVNAK+d7Cr3mK6o8CtSCT</span>
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        require.config({
            waitSeconds: 30,
            paths: {
                "App": "/out"
            }
        });
        require(["App"], function (remotely) {
            remotely.App.Pages.ApiTokens.Init();
        });
    </script>

}
else
{
    <h5 class="text-muted">Only organization administrators can view this page.</h5>
}
